package com.sihai.ehr.web.action.ajax;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.ExamQuanItemSet;
import com.sihai.ehr.model.biz.ExamQuanScoreDetail;
import com.sihai.ehr.service.ExamQuanDataInputService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.ArrayList;
import java.util.List;

/**
 * 定量考核Action
 * 
 * @author wxn
 * 
 */
public class ExamQuanAction extends EHRAction {

	@BeanInject
	private ExamQuanDataInputService examQuanDataInputService;

    private String year;
    private String itemIds;
    private List[] chartDatas;


	@Action(value = "/ajax/quan_chart_item_history", results = { @Result(name = "success", type = "json", params = {"root", "chartDatas" }) })
	public String getExamQuanChartByItems() throws Exception {
		try {
            Long workId = NumberUtils.createLong(getParameter("work_id"));
            if(workId == null || workId <= 0){
                return SUCCESS;
            }
            if(StringUtils.isNotBlank(itemIds)){
                String[] ids = itemIds.split(",");
                if(ids != null && ids.length > 0){
                    List<String> itemNames = new ArrayList<String>();
                    List<String> periodNames = new ArrayList<String>();
                    List<List<Float>> scores = new ArrayList<List<Float>>();
                    boolean isAddPeriod = false;
                    for(String id : ids){
                        Long itemId = NumberUtils.createLong(id);
                        List<ExamQuanScoreDetail> examQuanScoreDetails = examQuanDataInputService.findExamQuanScoreDetails(workId, year, itemId, createLimit(), createSorter());
                        if(examQuanScoreDetails != null && !examQuanScoreDetails.isEmpty()){
                            List<Float> scoreList = new ArrayList<Float>();
                            boolean isAddName = false;
                            for(ExamQuanScoreDetail  detail : examQuanScoreDetails){
                                if(!isAddName){
                                    itemNames.add(detail.getExamQuanItemSet().getTitle());
                                    isAddName = true;
                                }
                                scoreList.add(detail.getScore());
                                if(!isAddPeriod){
                                    periodNames.add(detail.getEmbedPeriod().getPeriodName());
                                }
                            }
                            if(periodNames != null && !periodNames.isEmpty()){
                                isAddPeriod = true;
                            }
                            scores.add(scoreList);
                        }else{
                            ExamQuanItemSet item = examQuanDataInputService.getExamQuanItemSet(itemId);
                            itemNames.add(item.getTitle());
                            List<Float> temp = new ArrayList<Float>(1);
                            temp.add(0.0f);
                            scores.add(temp);
                        }
                    }
                    chartDatas = new ArrayList[3];
                    chartDatas[0] = itemNames;
                    if(periodNames.isEmpty()){
                        periodNames.add(year);
                    }
                    chartDatas[1] = periodNames;
                    chartDatas[2] = scores;
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

    public void setYear(String year) {
        this.year = year;
    }

    public void setItemIds(String itemIds) {
        this.itemIds = itemIds;
    }

    public List[] getChartDatas() {
        return chartDatas;
    }
}
